package com.jyy.prefabricated.service;

import com.jyy.prefabricated.dto.SysUserDto;
import com.jyy.prefabricated.entity.SysUser;
import com.jyy.prefabricated.entity.TrainingOrganization;
import com.jyy.prefabricated.entity.TrainingOrganizationRegister;
import com.jyy.prefabricated.enums.ApplyStatusEnum;
import com.jyy.prefabricated.enums.BaseStatusEnum;
import com.jyy.prefabricated.enums.UserTypeEnum;
import com.jyy.prefabricated.exception.CustomException;
import com.jyy.prefabricated.mapper.TrainingOrganizationMapper;
import com.jyy.prefabricated.mapper.TrainingOrganizationRegisterMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * @version 1.0.0
 * @author: zjj
 */

@Service
public class TrainingOrganizationRegisterService {

    @Autowired
    private TrainingOrganizationRegisterMapper trainingOrganizationRegisterMapper;

    @Autowired
    private TrainingOrganizationService trainingOrganizationService;

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private TrainingOrganizationMapper trainingOrganizationMapper;

    /**
     * 新增
     *
     * @param trainingOrganizationRegister
     * @return
     */
    public Integer insert(TrainingOrganizationRegister trainingOrganizationRegister) {
        //注册校验手机号
        SysUser checkUser = sysUserService.selectByUsername(trainingOrganizationRegister.getTel());
        if (checkUser != null) {
            throw new CustomException("当前手机号已存在");
        }

//        trainingOrganizationRegister.setApplyStatus(ApplyStatusEnum.SUCCESS.getValue());
//        trainingOrganizationRegisterMapper.insert(trainingOrganizationRegister);

        TrainingOrganization trainingOrganization = new TrainingOrganization();
        BeanUtils.copyProperties(trainingOrganizationRegister, trainingOrganization);
        trainingOrganization.setStatus(BaseStatusEnum.VALID.getValue());
        trainingOrganization.setAccount(trainingOrganizationRegister.getTel());
        //生成相关系统账号,这里先生成系统账号判断账号是否已存在
        SysUserDto user = generateUser(trainingOrganization);
        //TODO 邀请人注册
        user.setPassword(trainingOrganizationRegister.getPassword());
        sysUserService.createUser(user);
        trainingOrganizationMapper.insert(trainingOrganization);
        return trainingOrganization.getId();
    }

    /**
     * 通过机构信息生成相关账号
     *
     * @param dto
     * @return
     */
    private SysUserDto generateUser(TrainingOrganization dto) {
        SysUser checkUser = sysUserService.selectByUsername(dto.getTel());
        if (checkUser != null) {
            throw new CustomException("当前手机号已存在");
        }
        SysUserDto user = new SysUserDto();
        user.setUsername(dto.getTel());
        user.setChannelCode(dto.getChannelCode());
        user.setInviter(dto.getChannelCode());
        user.setName(dto.getName());
        user.setMobile(dto.getTel());
        user.setEmail(dto.getEmail());
        user.setUserType(UserTypeEnum.TRAININGORGANIZATION.getValue());
        user.setPassword(dto.getTel());
        user.setStatus(BaseStatusEnum.VALID.getValue());
        return user;
    }

    /**
     * 更新
     *
     * @param trainingOrganizationRegister
     * @return
     */
    public int update(TrainingOrganizationRegister trainingOrganizationRegister) {
        if (trainingOrganizationRegister.getId() == null) {
            throw new CustomException("参数id不能为空");
        }
        return trainingOrganizationRegisterMapper.updateByPrimaryKeySelective(trainingOrganizationRegister);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    public int delete(Integer id) {
        return trainingOrganizationRegisterMapper.deleteByPrimaryKey(id);
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    public TrainingOrganizationRegister selectById(Integer id) {
        return trainingOrganizationRegisterMapper.selectByPrimaryKey(id);
    }

    /**
     * 通过
     *
     * @param id
     */
    @Transactional
    public void apply(Integer id, SysUser user) {
        TrainingOrganizationRegister register = trainingOrganizationRegisterMapper.selectByPrimaryKeyWithPwd(id);
        register.setApplyUserId(user.getId());
        register.setApplyStatus(ApplyStatusEnum.SUCCESS.getValue());
        update(register);
        trainingOrganizationService.applyRegister(register);
    }

    /**
     * 驳回
     *
     * @param id
     */
    public void reject(Integer id, SysUser user) {
        TrainingOrganizationRegister register = trainingOrganizationRegisterMapper.selectByPrimaryKey(id);
        register.setApplyUserId(user.getId());
        register.setApplyStatus(ApplyStatusEnum.FAIL.getValue());
        update(register);
    }

    /**
     * 根据参数查询列表
     *
     * @param keyword     关键字
     * @param startDate   开始日期
     * @param endDate     结束日期
     * @param applyStatus 审核状态
     * @return
     */
    public List<TrainingOrganizationRegister> selectByParams(String keyword, String startDate, String endDate, Integer applyStatus) {
        return trainingOrganizationRegisterMapper.selectByParams(keyword, startDate, endDate, applyStatus);
    }
}
